<template>
	<view class="soupItem" @click="goEdit">
		<!-- 顶部的内容 -->
		<view class="head">
			<view class="left">
				<soup-tab-group :type="item.soup_type"></soup-tab-group>
			</view>
			<!-- 跳转区域覆盖需要进行组织事件冒泡 处理 -->
			<view class="right" @click.stop="goDetail">
				<uni-icons type="eye" size="18" color="#999"></uni-icons>
				<text>详情</text>
			</view>
		</view>
		<!-- 主体内容 -->
		<view class="content">
			{{item.content}}
		</view>
		<!-- 底部的时间 和 功能区域 -->
		<view class="info">
			<view class="left">
				<view class="time">
					<uni-dateformat :date="item.publish_date" format="yyyy-MM-dd hh:mm"></uni-dateformat>
				</view>
			</view>
			<view class="right">
				<view class="group" v-if="stateFormat(item.status).value==1">
					<view class="every">
						<uni-icons type="heart" color="#a7a7a7" size="16"></uni-icons>
						<text class="font" v-if="item.like_count">{{item.like_count}}</text>
					</view>
					<view class="every">
						<uni-icons type="star" color="#a7a7a7" size="16"></uni-icons>
						<text class="font" v-if="item.collect_count">{{item.collect_count}}</text>
					</view>
					<view class="every">
						<uni-icons type="chatbubble" color="#a7a7a7" size="16"></uni-icons>
						<text class="font" v-if="item.comment_count">{{item.comment_count}}</text>
					</view>
				</view>
				<view v-else :style="{color:stateFormat(item.status).color}" class="feedback">
					{{stateFormat(item.status).text}}
					<view class="icons" v-if="item.status === 2" @click.stop="clickRemove(item._id)">
						<uni-icons type="trash" size="18"></uni-icons>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		getCurrentInstance
	} from 'vue'
	import {
		stateFormat
	} from '../../utils/common'

	const {
		proxy
	} = getCurrentInstance()

	//自定义事件
	const emits = defineEmits(['remove'])

	//引入数据库
	const db = uniCloud.database()
	//接收来自父组件的数据
	const props = defineProps({
		item: {
			type: Object,
			default () {
				return {}
			}
		},
		isEdit: {
			type: Boolean,
			default: true
		}
	})
	//点击跳转到编辑页面
	const goEdit = () => {
		if (!props.isEdit) return;
		uni.navigateTo({
			url: "/pages_self/soup/edit?id=" + props.item._id
		})
	}
	//点击跳转到详情页面
	const goDetail = () => {
		uni.navigateTo({
			url: "/pages/detailSoup/detailSoup?id=" + props.item._id
		})
		// console.log("已经跳转到详情页面");
	}

	//删除每一条数据
	const clickRemove = async (id) => {
		let res = await uni.showModal({
			title: "是否删除此条数据！",
			content: "删除之后无法恢复，请谨慎操作！"
		})
		// console.log(res);
		if (res.confirm) {
			let {
				result: {
					errCode
				}
			} = await db.collection('soup-jt').where(`_id == "${id}"`).update({
				is_delete: true
			})

			if (errCode != 0) return proxy.showToast('删除失败！')
			proxy.showToast('删除成功！')
			//触发自定义事件
			emits("remove")
		}
	}
</script>

<style lang="scss" scoped>
	.soupItem {
		border: 1px solid #e4e4e4;
		border-bottom-width: 3px;
		margin-bottom: 40rpx;
		border-radius: 10rpx;

		.head {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx;

			.right {
				font-size: 28rpx;
				color: $text-font-color-3;
				display: flex;
				line-height: 1em;
				align-items: center;
				// border: 1rpx solid red;
				padding: 20rpx 30rpx;

				text {
					margin-left: 10rpx;
				}
			}
		}

		.content {
			padding: 30rpx 20rpx;
			font-size: 40rpx;
			font-weight: bold;
			line-height: 1.7em;
			// 	text-align: justify; 表示两端对齐
			text-align: justify;
		}

		.info {
			background-color: #f9f9f9;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx;
			color: $text-font-color-3;

			.group {
				display: flex;
				align-items: center;

				.every {
					margin-left: 20rpx;

					.font {
						font-size: 26rpx;
					}
				}
			}

			.feedback {

				display: flex;
				align-items: center;

				.icons {
					// border: 1rpx solid red;
					padding: 5rpx 10rpx;
					margin-left: 10rpx;
				}
			}
		}

	}
</style>